package atguigu.dao.impl;

import atguigu.dao.AccountDao;
import atguigu.pojo.Account;
import atguigu.utils.ConnectionManager;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
@Repository
public class AccountDaoImpl implements AccountDao {
    //操作数据库，依赖QueryRunner类
    @Autowired
    private QueryRunner qr;
    //QueryRunner对象执行SQL语句的时候，保证事务的安全
    //不能让QueryRunner自己获取数据库连接，和控制事务的连接是一个对象
    @Autowired
    private ConnectionManager connectionManager;

    @Override
    public int updateAccount(Account account) throws SQLException {
        String sql= "update account set money = ? where name = ?";
        return qr.update(connectionManager.getConnection(),sql,account.getMoney(),account.getName());
    }

    @Override
    public Account queryAccount(String name) throws SQLException {
        String sql = "select * from account where name =?";
        return qr.query(connectionManager.getConnection(),sql,new BeanHandler<Account>(Account.class),name);
    }
}
